package com.huadi.firematerialident.service;

import com.huadi.firematerialident.dao.IdentifierDaoImpl;
import com.huadi.firematerialident.dao.UserDaoImpl;
import com.huadi.firematerialident.po.Admin;
import com.huadi.firematerialident.po.Identifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class IdentifierServiceImpl implements IdentifierService{
    @Autowired
    IdentifierDaoImpl identDao;
    @Autowired
    UserDaoImpl userDao;
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public int addIdentifier(Identifier ident) {
        return identDao.addIdentifier(ident);
    }

    @Override
    public int deleteIdentifierByID(String identID) {
        return identDao.deleteIdentifierByID(identID);
    }

    @Override
    public int updateIdentifier(Identifier ident) {
        return identDao.updateIdentifier(ident);
    }

    @Override
    public List<Map<String,Object>> selectIdentifierByID(String identID) {
        return identDao.selectIdentifierByID(identID);
    }

    @Override
    public List<Map<String,Object>> getAllIdentifier() {
        return identDao.getAllIdentifier();
    }

    @Override
    public String getIdentID(){
        String IdentCount = userDao.getUsername();
        List<Map<String,Object>> list = jdbcTemplate.queryForList("select IdentID,IdentCount from identifier");
        for(int i=0;i<list.size();i++) {
            if (IdentCount.equals(list.get(i).get("IdentCount"))) {
                return (String) list.get(i).get("IdentID");
            }
        }
        return null;
    }

    @Override
    public Identifier selectIdentifierByID_class(String id){
        List<Map<String ,Object>> list = identDao.selectIdentifierByID(id);
        Identifier ident = new Identifier();
        ident.setIdentID((String) list.get(0).get("IdentID"));
        ident.setIdentCount((String) list.get(0).get("IdentCount"));
        ident.setIdentPassword((String) list.get(0).get("IdentPassword"));
        ident.setIdentName((String) list.get(0).get("IdentName"));
        ident.setIdentSex((String) list.get(0).get("IdentSex"));
        ident.setIdentBirth((LocalDate) list.get(0).get("IdentBirth"));
        ident.setIdentPhone((String) list.get(0).get("IdentPhone"));
        ident.setLogicalAddDel((boolean) list.get(0).get("LogicalAddDel"));
        ident.setLastModification((LocalDateTime) list.get(0).get("LastModification"));
        return ident;
    }
}
